/*Copyright ©2015 TommyLemon(https://github.com/TommyLemon)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/

package zblibrary.facechat.application;

import android.util.Log;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;

import java.util.HashMap;
import java.util.Map;

import lingxi.common.config.Api;
import lingxi.common.okhttp.OkUtil;
import lingxi.common.util.SPUtil;
import okhttp3.OkHttpClient;
import zblibrary.facechat.manager.DataManager;
import zblibrary.facechat.viewmodel.impl.ZUser;
import zuo.biao.library.base.BaseApplication;
import zuo.biao.library.util.StringUtil;

/**Application
 * @author Lemon
 */
public class FaceChatApplication extends BaseApplication {
	private static final String TAG = "DemoApplication";

	private static FaceChatApplication context;
	public static FaceChatApplication getInstance() {
		return context;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		context = this;

		// 一定要注册
		SPUtil.newInstance().init(this);
		// 根据需求使用
		initOkUtil();
		OkHttpClient mOkHttpClient = new OkHttpClient();
		ImagePipelineConfig imagePipelineConfig = OkHttpImagePipelineConfigFactory
				.newBuilder(this, mOkHttpClient)
				.setDownsampleEnabled(true)
				.build();
		Fresco.initialize(this, imagePipelineConfig);
	}

	/**
	 * 初始化OkUtil
	 */
	private void initOkUtil() {
		// 公共请求头
		Map<String, String> headers = new HashMap<>(1);
		String token = SPUtil.build().getString(Api.X_APP_TOKEN);
		headers.put(Api.X_APP_TOKEN, token);
		// 注册添加公共请求头
		OkUtil.newInstance().init(this)
				.addCommonHeaders(headers);
	}
	
	/**获取当前用户id
	 * @return
	 */
	public long getCurrentUserId() {
		currentUser = getCurrentUser();
		Log.d(TAG, "getCurrentUserId  currentUserId = " + (currentUser == null ? "null" : currentUser.getId()));
		return currentUser == null ? 0 : currentUser.getId();
	}
	/**获取当前用户phone
	 * @return
	 */
	public String getCurrentUserPhone() {
		currentUser = getCurrentUser();
		return currentUser == null ? null : currentUser.getPhone();
	}


	private static ZUser currentUser = null;
	public ZUser getCurrentUser() {
		if (currentUser == null) {
			currentUser = DataManager.getInstance().getCurrentUser();
		}
		return currentUser;
	}

	public void saveCurrentUser(ZUser user) {
		if (user == null) {
			Log.e(TAG, "saveCurrentUser  currentUser == null >> return;");
			return;
		}
		if (user.getId() <= 0 && StringUtil.isNotEmpty(user.getName(), true) == false) {
			Log.e(TAG, "saveCurrentUser  user.getId() <= 0" +
					" && StringUtil.isNotEmpty(user.getName(), true) == false >> return;");
			return;
		}

		currentUser = user;
		DataManager.getInstance().saveCurrentUser(currentUser);
	}

	public void logout() {
		currentUser = null;
		DataManager.getInstance().saveCurrentUser(currentUser);
	}
	
	/**判断是否为当前用户
	 * @param userId
	 * @return
	 */
	public boolean isCurrentUser(long userId) {
		return DataManager.getInstance().isCurrentUser(userId);
	}

	public boolean isLoggedIn() {
		return getCurrentUserId() > 0;
	}



}
